// pages/orderlist/orderdetail.js
var utl = require('../../utils/util.js');
var mdk = require('../../utils/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order:{},
    roads:[],  //通道
    cards:[],  //银行卡
    cardnum:'',
    showAlt:false,
    paypassw:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var od = options.order;
    var tt = JSON.parse(od);
    this.setData({
      order:tt
    })
    console.log('订单详情'+od);
    if(od){
      this.checkgold();
    }
  },
  //密码后支付
  payAction:function(){
    var pwd = this.data.paypassw;
    var cardno = this.data.cardnum;
    var ode = this.data.order;
    var road = this.data.roads;
    var rindex = this.data.roadIndex;
    var secr = mdk.hexMD5(pwd);
    var err='';
    console.log('支付通道为啥'+rindex);
    if(rindex == undefined){
      err = '请选择通道';
    }
    if(cardno==''){
      err = '请选择支付卡';
    }
    if(pwd == ''){
      err = '密码不为空';
    }
    if(!ode){
      err='订单不为空';
    }
    if(err !=''){
      wx.showToast({
        title: err,
        icon:'none',
        duration:2000
      })
      return;
    }
    var rd = road[rindex];
    
    utl.serverToken('USER_PAY', { 'CARD_NO': cardno, 'PASSWORD': secr, 'GATHERING_ID': String(ode.id), 'PROD_ID': rd.prod_id, 'TUNNEL_ID': rd.tunnel_id, 'AMOUNT': ode.money, 'GOODS': ode.node }, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000
        })
        return;
      }
      console.log(res);
      var result = JSON.stringify(res);
     
       wx.navigateTo({
          url: '/pages/orderlist/payResult?result='+result,
        })

    })
  },
  //密码输入框
  passwinpt:function(e){
    this.setData({
      paypassw:e.detail.value,
    })
  },
  seleck:function(){
    this.setData({
      showAlt:true
    })
  },
  //关闭输入密码
  cancleAlt:function(){
    this.setData({
      showAlt:false
    })
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    var rsd = this.data.cards;
    var index = e.detail.value;
    this.setData({
      cardnum: rsd[index].card_no
    })
  },
  //点击通道
  tapRoadCell:function(e){
    var index = e.currentTarget.dataset.info;
    this.setData({
      roadIndex:index
    })
  },
  //支持银行
  supportBank:function(e){
    var index = e.currentTarget.dataset.info;
    var road = this.data.roads[index];
    var turnid = road.tunnel_id;
    var productid = road.prod_id;
    wx.navigateTo({
      url: '/pages/orderlist/bankSupport?turnid='+turnid+'&product='+productid,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  //查询通道
  checkgold:function(){
    wx.showLoading({
      title: '请求中...',
    })
    var md = this.data.order;
    utl.serverToken('USER_PROD_CONF', { 'GATHERING_ID': md.id, 'TRADE_TYPE': '1', 'AMOUNT': md.money }, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000
        })
        return;
      }
     console.log(res);
      this.setData({
        roads:res.DATA,
      })
    })
  },
  //绑定信用卡
  bindcardAction:function(){
    wx.navigateTo({
      url: '/pages/openquick/index',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.checkCard();
  },
  //查询本人银行卡
  checkCard:function(){
    utl.serverToken('USER_CARD', {}, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000
        })
        return;
      }
   
      this.setData({
        cards: res.DATA,
      })
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})